﻿using CSRedis;
using System;
using System.Collections.Generic;
using System.Text;

namespace SystemCenterSoa.BaseLibrary.Tools.HelperTools
{
    public class MyRedisHelper
    {
        //public static RedisManager RedisManagerObj
        //{
        //    get
        //    {
        //        return DiManager.GetDiObj<RedisManager>();
        //    }
        //}

        //private IConfiguration _configuration;
       // private readonly CSRedisClient _csredis;
     //   public RedisManager(IConfiguration configuration)
        //public RedisManager(IConfiguration configuration)
        //{
        //   // _configuration = configuration;
        //    //  _csredis = new CSRedis.CSRedisClient("127.0.0.1:6379,password=123,defaultDatabase=13,poolsize=50,ssl=false,writeBuffer=10240,prefix=key前辍");
        //  //  _csredis = new CSRedisClient(_configuration.GetSection("ConnectionStrings")["DefaultRedisConnection"]);
        //  //  RedisHelper.Initialization(_csredis);

        //}
        private static int defaultTimeExpire = 86400;//默认过期秒数

        private static void Dispose()
        {
            RedisHelper.Instance.Dispose();
          //  GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 保存数据DB文件到硬盘
        /// </summary>
        private static void Save()
        {
            RedisHelper.NodesServerManager.Save();

        }

        /// <summary>
        /// 异步保存数据DB文件到硬盘
        /// </summary>
        private static void SaveAsync()
        {
            RedisHelper.NodesServerManager.SaveAsync();
            
        }

        #region /// redis操作方法

        /// <summary>
        /// 判断某个数据是否已经被缓存
        /// </summary>
        public static bool Exist(string key)
        {
            try
            {
                var result = RedisHelper.Exists(key);

                return result;
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 存储数据到redis
        /// </summary>
        public static bool Set(string key, object value)
        {
            try
            {

                RedisHelper.Set(key, value, defaultTimeExpire);

            }
            catch
            {
                return false;
            }
            return true;
        }



        /// <summary>
        /// 存储数据到redis
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeExpire"></param>
        /// <returns></returns>
        public static bool Set(string key, object value, int timeExpire)
        {
            try
            {
                RedisHelper.Set(key, value, timeExpire);
            }
            catch
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 存储数据到hash表
        /// </summary>
        public static bool HSet(string key, string f, object vaule)
        {
            try
            {

                RedisHelper.HSet(key, f, vaule);
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 存储数据到hash表
        /// </summary>
        public static bool HSet(string key, string f,object vaule, int timeExpire)
        {
            try
            {
               
                RedisHelper.HSet(key, f,vaule);
                RedisHelper.Expire(key, timeExpire);
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 存储数据到hash表
        /// </summary>
        public static bool HGet(string key, string f,object value)
        {
            try
            {
                RedisHelper.HGet(key, f);

            }
            catch(Exception ex)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 存储数据到hash表
        /// </summary>
        public static T HGet<T>(string key, string f)
        {
            try
            {
               return RedisHelper.HGet<T>(key, f);

            }
            catch (Exception ex)
            {
                return default(T);
            }
        }
        /// <summary>
        /// 移除hash中的某值
        /// </summary>
        public static bool HRemove(string key, string f)
        {
            try
            {
                var result = RedisHelper.HDel(key,new string[] { f } );
                return result > 0;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 移除hash中的某值
        /// </summary>
        public static bool HRemove(string key, string[] f)
        {
            try
            {
                var result = RedisHelper.HDel(key, f);
                return result>0;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 根据key获取数据
        /// </summary>
        public static string Get(string key)
        {
            try
            {
                var value = RedisHelper.Get<string>(key);
                return value;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 存取任意类型的值
        /// </summary>
        public static T Get<T>(string key)
        {
            try
            {
                var result = RedisHelper.Get<T>(key);
                return result;
            }
            catch(Exception ex)
            {
                return default(T);
            }
        }
        /// <summary>
        /// 刷新过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="timeExpire"></param>
        /// <returns></returns>
        public static bool Expire(string key, int timeExpire)
        {
            return RedisHelper.Expire(key, timeExpire);
        }
        /// <summary>
        /// 移除key的value
        /// </summary>
        public static bool Remove(string key)
        {
            try
            {
                var result = RedisHelper.Del(key);
                return result>0;
            }
            catch
            {
                return false;
            }
        }

       


        #endregion
    }
}
